
package com.qf.edu.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc:进入后的关于登录验证的过滤器
 * @datatime: 2022/5/24 11:37
 */

public class CheckFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html;charset=utf-8");
        // 获取session对象
        HttpSession session = request.getSession();
        //获取登录控制器的页面数据
        Object user = session.getAttribute("user");
        if (user != null) {
            filterChain.doFilter(request,response);
        } else {
            response.sendRedirect("/login.html");
        }
    }

    @Override
    public void destroy() {

    }

}

